import 'package:dio/dio.dart';
import 'package:fsharing/core/services/auth.dart';
import 'package:fsharing/core/viewmodels/view_model.dart';

class RegPasswordResetModel extends ViewModel {
  final Auth auth;
  String username = '';
  String mobile = '';
  String checkCode = '';
  String password = '';
  String passwordConfirm = '';
  String _errorMessage;

  RegPasswordResetModel(this.auth) : super();

  String get errorMessage => _errorMessage;

  Future<ViewState> regPasswordReset() async {
    viewState = ViewState.busy;
    try {
      if (username.isEmpty) {
        throw '请输入用户名';
      }
      if (mobile.isEmpty) {
        throw '请输入手机号';
      }
      if (checkCode.isEmpty) {
        throw '请输入验证码';
      }
      if (password.isEmpty) {
        throw '请输入密码';
      }
      if (password != passwordConfirm) {
        throw '确认密码不一致';
      }
      var params = <String, dynamic>{};
      params['checkCode'] = checkCode;
      params['mobile'] = mobile;
      params['newPassword'] = passwordConfirm;
      params['username'] = username;
      await auth.regPasswordReset(params);
      viewState = ViewState.success;
    } catch (e) {
      _errorMessage = e is DioError ? e.message : e.toString();
      viewState = ViewState.error;
    }
    return viewState;
  }
}
